<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        class promise {
            pending = 'pending'
            success = "resolve"
            fail = "reject"
            res_event_loop = []
            rej_event_loop = []

            constructor(callback) {
                this.value = null;
                this.status = 'pending'
                callback(this.resolve, this.reject)
            }

            resolve = (value) => {
                console.log(this.constructor)
                if (this.status === 'pending') {
                    this.status = this.success
                    setTimeout(() => {
                        this.res_event_loop.forEach(res => {
                            res(this.value)
                        })

                    })
                    this.value = value
                }

            }

            reject = (value) => {
                if (this.status === 'pending') {
                    this.status = this.fail
                    setTimeout(() => {
                        this.rej_event_loop.forEach(rej => {
                            rej(this.value)
                        })
                    })
                    this.value = value
                }
            }

            then = (resolve, reject) => {
                console.log(this.status)
                if (this.status === this.success) {
                    resolve(this.value)
                }
                if (this.status === this.fail) {
                    reject(this.value)
                }
                if (this.status === this.pending) {
                    this.res_event_loop.push(resolve)
                    this.rej_event_loop.push(reject)
                }
            }
        }


        new promise((resolve, reject) => {
            resolve(123)
            reject(456)
        }).then((value) => {
            console.log(value)
        }, (err) => {
            console.log(err)
        })
    </script>
</head>
<body>

</body>
</html>
